package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsAttributeMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.GoodsAttributeService;
import com.qf.service.GoodsProductService;
import com.qf.service.GoodsService;
import com.qf.service.GoodsSpecificationService;
import com.qf.vo.GoodsAllinone;
import com.qf.vo.GoodsStat;
import com.qf.vo.MyInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;
    @Autowired
    private GoodsAttributeService goodsAttributeService;
    @Autowired
    private GoodsProductService goodsProductService;
    @Autowired
    private GoodsSpecificationService goodsSpecificationService;


    @Override
    public Long findGoodsCount() {
        Long aLong = dtsGoodsMapper.selectCount(null);
        return aLong;

    }

    @Override
    public IPage<DtsGoods> findListByQuery(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        return dtsGoodsMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsGoods>()
                .like(!StringUtils.isEmpty(goodsSn),"goods_sn",goodsSn)
                .like(!StringUtils.isEmpty(name),"name",name)
                .eq("deleted",false)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public List<Integer> findGoodsCategory(Integer id) {
        MyInteger goodsCategory = dtsGoodsMapper.findGoodsCategory(id);
        Integer p = goodsCategory.getPid();
        Integer c = goodsCategory.getId();
        List<Integer> categoryIds=new ArrayList<>();
        categoryIds.add(p);
        categoryIds.add(c);
        return categoryIds;
    }

    @Override
    public DtsGoods findOneGoods(Integer id) {
        return dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>()
                .eq("id",id)
                .eq("deleted",false));
    }

    @Transactional
    @Override
    public void updateAll(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        goodsAttributeService.update(attributes,goods.getId());
        goodsSpecificationService.update(specifications,goods.getId());
        goodsProductService.update(products,goods.getId());
        dtsGoodsMapper.updateById(goods);





    }

    @Transactional
    @Override
    public void createGoods(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        goods.setBrowse(0);
        goods.setSales(0);
        goods.setApproveStatus(0);
        goods.setBrokerageType(0);
        goods.setSortOrder(100);
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        dtsGoodsMapper.createGoods(goods);
        goodsAttributeService.create(attributes,goods.getId());
        goodsSpecificationService.create(specifications,goods.getId());
        goodsProductService.create(products,goods.getId());

    }

    @Override
    public void deleteGoods(DtsGoods dtsGoods) {
        dtsGoodsMapper.deleteByGoodsId(dtsGoods.getId());
    }

    @Override
    public List<GoodsStat> findOrderStat() {
        return dtsGoodsMapper.findOrderStat();
    }
}
